package com.liwenfeng.diancanspringboot.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.liwenfeng.diancanspringboot.common.constant.StatusConstant;
import com.liwenfeng.diancanspringboot.common.exception.DeletionNotAllowedException;
import com.liwenfeng.diancanspringboot.common.result.PageResult;
import com.liwenfeng.diancanspringboot.mapper.CategoryMapper;
import com.liwenfeng.diancanspringboot.mapper.DishMapper;
import com.liwenfeng.diancanspringboot.mapper.SetmealDishMapper;
import com.liwenfeng.diancanspringboot.mapper.SetmealMapper;
import com.liwenfeng.diancanspringboot.pojo.dto.SetmealDTO;
import com.liwenfeng.diancanspringboot.pojo.dto.SetmealPageQueryDTO;
import com.liwenfeng.diancanspringboot.pojo.entity.Category;
import com.liwenfeng.diancanspringboot.pojo.entity.Setmeal;
import com.liwenfeng.diancanspringboot.pojo.entity.SetmealDish;
import com.liwenfeng.diancanspringboot.pojo.vo.DishItemVO;
import com.liwenfeng.diancanspringboot.pojo.vo.SetmealVO;
import com.liwenfeng.diancanspringboot.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmealMapper.update(setmeal);
        setmealDishMapper.delete(List.of(setmealDTO.getId()));
        List<SetmealDish> setmealDishs = setmealDTO.getSetmealDishes();

        if (setmealDishs != null && setmealDishs.size() > 0) {
            setmealDishs.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
            setmealDishMapper.insertBatch(setmealDishs);
        }
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page = setmealMapper.selectByPage(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void changeStatus(Integer status, Long id) {
        if (status == 1) {
            List<SetmealDish> setmealDishs = setmealDishMapper.getBySetmealId(id);
            int count = dishMapper.countByStatus(setmealDishs.stream().map(SetmealDish::getDishId).toList(), 0);
            if (count > 0) {
                // 如果有停售菜品，抛出业务异常
                throw new DeletionNotAllowedException("套餐中存在停售菜品，无法启售");
            }
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            Setmeal byId = setmealMapper.getById(id);
            if (Objects.equals(byId.getStatus(), StatusConstant.ENABLE)) {
                // 如果有启售套餐，抛出业务异常
                throw new DeletionNotAllowedException("启售状态的套餐，不能删除");
            }
        }
        setmealMapper.delete(ids);
        setmealDishMapper.delete(ids);
    }

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
        List<SetmealDish> setmealDishs = setmealDTO.getSetmealDishes();
        if (setmealDishs != null && setmealDishs.size() > 0) {
            setmealDishs.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
            setmealDishMapper.insertBatch(setmealDishs);
        }
    }

    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.getById(id);
        if (setmeal != null) {
            BeanUtils.copyProperties(setmeal, setmealVO);
            setmealVO.setSetmealDishes(setmealDishMapper.getBySetmealId(id));
            Category category = categoryMapper.getById(setmeal.getCategoryId());
            setmealVO.setCategoryName(category.getName());
            return setmealVO;
        }
        return null;
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    @Override
    public List<SetmealVO> listWithDishList(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        if (list != null && list.size() > 0) {
            return list.stream().map(item -> {
                SetmealVO setmealVO = new SetmealVO();
                BeanUtils.copyProperties(item, setmealVO);
                setmealVO.setSetmealDishes(setmealDishMapper.getBySetmealId(item.getId()));
                return setmealVO;
            }).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    @Override
    public List<SetmealVO> listByCategoryId(long categoryId) {
        Setmeal setmeal = new Setmeal();
        setmeal.setCategoryId(categoryId);
        setmeal.setStatus(StatusConstant.ENABLE);
        List<Setmeal> setmeals = setmealMapper.list(setmeal);
        if (setmeals == null || setmeals.size() == 0) {
            return null;
        }
        return setmeals.stream().map(item -> {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(item, setmealVO);
            setmealVO.setSetmealDishes(setmealDishMapper.getBySetmealId(item.getId()));
            return setmealVO;
        }).collect(Collectors.toList());
    }
}
